package com.onpositive.gamemaker.editor.prefs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import com.onpositive.gamemaker.editor.GameCreatorEditorPlugin;

public class GameCreatorPrefs
{
	public static final String DEFAULT_SPRITE_WIDTH = "DEFAULT_SPRITE_WIDTH";
	public static final String DEFAULT_SPRITE_HEIGHT = "DEFAULT_SPRITE_HEIGHT";
	public static final String SAVED_PATHS = "SAVED_PATHS";
	public static final String LAST_PATH = "LAST_PATH";
	public static final String filePathSeparator = "|";
	
	public static int getDefaultSpriteWidth()
	{
		return GameCreatorEditorPlugin.getDefault().getPreferenceStore().getInt(DEFAULT_SPRITE_WIDTH);
	}
	
	public static void setDefaultSpriteWidth(int width)
	{
		GameCreatorEditorPlugin.getDefault().getPreferenceStore().setValue(DEFAULT_SPRITE_WIDTH,width);
	}
	
	public static int getDefaultSpriteHeight()
	{
		return GameCreatorEditorPlugin.getDefault().getPreferenceStore().getInt(DEFAULT_SPRITE_HEIGHT);
	}
	
	public static void setDefaultSpriteHeight(int height)
	{
		GameCreatorEditorPlugin.getDefault().getPreferenceStore().setValue(DEFAULT_SPRITE_HEIGHT,height);
	}
	
	public static List<String> getSavedImagePaths()
	{
		String savedPaths = GameCreatorEditorPlugin.getDefault().getPreferenceStore().getString(SAVED_PATHS);
		ArrayList<String> result = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(savedPaths, filePathSeparator);
		while (st.hasMoreTokens())
			result.add(st.nextToken());
		return result;
	}
	
	public static void addSavedImagePath(String path)
	{
		List<String> savedPaths = getSavedImagePaths();
		if (savedPaths.contains(path))
			savedPaths.add(path);
	}
	
	public static void setSavedImagePaths(List<String> paths)
	{
		StringBuilder builder = new StringBuilder();
		for (Iterator<String> iterator = paths.iterator(); iterator.hasNext();)
		{
			String path = iterator.next();
			if (builder.length() > 0)
				builder.append(filePathSeparator);
			builder.append(path);
		}
		GameCreatorEditorPlugin.getDefault().getPreferenceStore().setValue(SAVED_PATHS, builder.toString());
	}
	
	public static void setLastPath(String path)
	{
		GameCreatorEditorPlugin.getDefault().getPreferenceStore().setValue(LAST_PATH,path);
	}
	
	public static String getLastPath()
	{
		return GameCreatorEditorPlugin.getDefault().getPreferenceStore().getString(LAST_PATH);
	}

}
